// 发布订阅模式
class PubSub {
    constructor() {
        this.subscribers = {};
    }

    subscribe(type, callback) {
        if (!this.subscribers[type]) {
            this.subscribers[type] = [];
        }
        this.subscribers[type].push(callback);
    }

    publish(type, ...args) {
        if (this.subscribers[type]) {
            this.subscribers[type].forEach(callback => {
                callback(...args);
            });
        }
    }

    unsubscribe(type, callback) {
        if (this.subscribers[type]) {
            this.subscribers[type] = this.subscribers[type].filter(cb => cb !== callback);
        }
    }
}

function eventA(value) {
    console.log('eventA', value);
}
function testB() {
    console.log('testB');
}

const pubSub = new PubSub();
pubSub.subscribe('eventA', eventA);
pubSub.subscribe('testB', testB);
pubSub.publish('eventA', 'hello world')
pubSub.publish('testB');